/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.props;

import java.util.ArrayList;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.pojo.module.local.Prop;
import com.alstudio.pojo.module.local.Props;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * 解析道具商城道具列表
 * 
 * @author alonso lee
 * 
 */
public class PropsListParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private ArrayList<Prop> proplist;
	private Props props;
	private ArrayList<Props> propsList;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		if (proplist != null) {
			proplist.clear();
			proplist = null;
		}
		props = null;
		proplist = new ArrayList<Prop>();
		propsList = new ArrayList<Props>();
		mListener = listener;
		startParse(packact, stream, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processErrorStartTag(java.lang.String)
	 */
	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processSetStartTag(java.lang.String)
	 */
	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processGetStartTag(java.lang.String)
	 */
	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processResultStartTag(java.lang.String)
	 */
	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEMS_TAG)) {
			props = new Props();
			props.setPropsIndex(ALNumericUtils
					.parseInt(getAttValue("index"), 1));
			props.setPropsType(getAttValue("type"));
			props.setPropsName(getAttValue("typename"));
			proplist = new ArrayList<Prop>();
		} else if (tag.equals(ITEM_TAG)) {
			Prop p = new Prop();
			p.setPropType(props.getPropsType());
			p.setPropDesc(getAttValue("desc"));
			p.setPropName(getAttValue("propname"));
			p.setPropLevel(ALNumericUtils.parseInt(getAttValue("level"), 1));
			p.setPropFileId(getAttValue("fileid"));
			p.setPropsIndex(props.getPropsIndex());
			p.setPropIndex(ALNumericUtils.parseInt(getAttValue("index"), 1));
			p.setProp(getAttValue("props"));
			proplist.add(p);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndTag(java.lang.String)
	 */
	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEMS_TAG)) {
			props.setPropitems(proplist);
			propsList.add(props);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processStartDocument()
	 */
	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndDocument()
	 */
	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser#onCallback
	 * ()
	 */
	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			mListener.onGetPropsMallListResult(code, propsList);
			ALLog.d("开始回调道具列表" + proplist.size());
		}
	}

}
